package com.radiusmess;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import net.sf.json.util.NewBeanInstanceStrategy;

import com.util.HexUtil;

public class RadiusParser {
	private byte[] acct_verify=new byte[16];
	private byte[] auth_verify=new byte[16];
	
	private byte[] mess;
	private ByteArrayOutputStream bStream = null;
	public  void getAuthVerify(byte[]mess,RadiusTester r,RadiusInformation ri) throws IOException{
		
		byte [] newmess = new byte [bytesToInt(mess, 3)];
		this.mess = new byte [bytesToInt(mess, 3)];
		System.out.println(newmess.length);
		for (int i = 0; i < newmess.length; i++) {
			newmess[i]=mess[i];
			this.mess[i]=mess[i];
		}
		System.out.println("newmess返回报文----"+HexUtil.chang(newmess));
		
		System.out.println("r.getAuth_authenticat()--"+HexUtil.chang(r.getAuth_authenticat()));
		for (int i = 0; i < 16; i++) {
			newmess[4+i]=r.getAuth_authenticat()[i];
		}
		System.out.println("newmess返回报文2----"+HexUtil.chang(newmess));
		 bStream=new ByteArrayOutputStream(1024);
		 bStream.write(newmess);
		 bStream.write(ri.getRadiusKey().getBytes());
		 MD5 md5 = new MD5();
		 md5.MD5_calc(auth_verify, bStream.toByteArray(), bStream.size());
		
	}
	
	public  void getAcctVerify(byte[]mess,RadiusTester r,RadiusInformation ri) throws IOException{
		
		byte [] newmess = new byte [bytesToInt(mess, 3)];
		this.mess = new byte [bytesToInt(mess, 3)];
		System.out.println(newmess.length);
		for (int i = 0; i < newmess.length; i++) {
			newmess[i]=mess[i];
			this.mess[i]=mess[i];
		}
		System.out.println("newmess返回报文----"+HexUtil.chang(newmess));
		
		
		for (int i = 0; i < 16; i++) {
			newmess[4+i]=r.getAcct_authenticat()[i];
		}
		 bStream=new ByteArrayOutputStream(1024);
		 bStream.write(newmess);
		 bStream.write(ri.getRadiusKey().getBytes());
		 MD5 md5 = new MD5();
		 md5.MD5_calc(acct_verify, bStream.toByteArray(), bStream.size());
	}
	
	
	
	private int bytesToInt(byte[] ary, int offset) {  
	    int value;    
	    value = (int) ((ary[offset]&0xFF)   
	            | ((ary[offset-1]<<8) & 0xFF00)); 
	             
	    return value;  
	}  
	
	
	
	
	
	
	
	public byte[] getMess() {
		return mess;
	}

	public void setMess(byte[] mess) {
		this.mess = mess;
	}

	public byte[] getAcct_verify() {
		return acct_verify;
	}
	public void setAcct_verify(byte[] acct_verify) {
		this.acct_verify = acct_verify;
	}
	public byte[] getAuth_verify() {
		return auth_verify;
	}
	public void setAuth_verify(byte[] auth_verify) {
		this.auth_verify = auth_verify;
	}
	
	
	
	
}
